package com.cn.wanxi.util;

import com.cn.wanxi.dao.CompanyDao;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class IOTool {
    /**
     * 引用类型的   强引用，弱引用，软引用，虚引用
     */
    private static  File file = null;

    //int c=0;
    //    private static final Long AA=4L;
// final 什么情况不改变值？产量的数据类型是基本类型
    public static void createNote(String fileName) {
//        int a=0;
//        System.out.println(a);
//        int b=0;
        File directory = new File(MyFileName.PATH);
//        directory.isDirectory();
        boolean exists = directory.exists();
//        System.out.println(exists);
//        System.out.println(file.isAbsolute());
//如果没有当前的路径，则直接进行创建文件,少些if else
        if (!exists) {
            directory.mkdirs();//创建文件夹
        }
        File filea = new File(directory, fileName);
        try {
            boolean newFile = filea.createNewFile();
//            System.out.println(newFile);
            file = filea;
        } catch (Exception e) {

        }

    }

    /**
     * 写入数据
     */
    public static void wirteFile(List<String> data) {
//            char value[] = logo.toCharArray();
//            for (char a : value
//            ) {
//                outputStream.write(a);
//            }
        try {
            OutputStream outputStream = new FileOutputStream(file);
            for (int i = 0; i < data.size(); i++) {
                outputStream.write(data.get(i).getBytes());
                outputStream.write(MyFileName.SPLIT.getBytes());
            }
//            CompanyDao companyDao = new CompanyDao();
////            String logo = companyDao.getLogo();
////            outputStream.write(logo.getBytes());
////            outputStream.write(MyFileName.SPLIT.getBytes());
////            outputStream.write(companyDao.getPhone().getBytes());
////            outputStream.write(MyFileName.SPLIT.getBytes());
////            魔法值 ，魔法值不能单独出现在某个地方，二十应该提取出来放到统一的类里面进行单独管理
////            outputStream.write(companyDao.getNet().getBytes(StandardCharsets.UTF_8));
////            outputStream.write(MyFileName.SPLIT.getBytes());
////            outputStream.write(companyDao.getEmail().getBytes());
////            outputStream.write(MyFileName.SPLIT.getBytes());
////            outputStream.write(companyDao.getAddress().getBytes());
////            outputStream.write(MyFileName.SPLIT.getBytes());
////            outputStream.write(companyDao.getVersion().getBytes());
////            outputStream.write(MyFileName.SPLIT.getBytes());
////            outputStream.write(companyDao.getLinePhone().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    /**
     * 读取数据
     */
    public static String readFile() {
//       String和 StringBuilder和StringBuffer的区别?
        StringBuilder result = new StringBuilder();
        try {
//            InputStream inputStream = new FileInputStream(file);
////            byte[] bytes=new byte[10];
////            for (int i = 0; i < file.length(); i++) {
//            int read = 0;
//            while ((read = inputStream.read()) != -1) {
////                int read = inputStream.read();
//                System.out.print((char) read);
//            }
            Reader reader = new FileReader(file);
            int read = 0;

            while ((read = reader.read()) != -1) {
//                System.out.print((char) read);
                result.append((char) read);
            }
//            System.out.println(result);
//            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }
}
